class Solution {
public:
    int sum(int n)
    {
        int tmp = n;
        int sum = 0;
        while(tmp)
        {
            sum += pow(tmp % 10, 2);
            tmp /= 10;
        }
        return sum;
    }

    bool isHappy(int n) {
        int slow = n, fast = sum(n); // 快慢双指针
        while(slow != fast)
        {
            slow = sum(slow);   // 慢指针走一步
            fast = sum(sum(fast));  // 快指针走两步
        }

        return slow == 1;
    }
};